package leetcode.ge100;

class Solution_122 {

	public static void main(String[] args) {
		int[] prices = { 7, 1, 5, 3, 6, 4 };
		new Solution_122().maxProfit(prices);
	}

	public int maxProfit(int[] prices) {
		int l = prices.length;
		if (l < 2)
			return 0;
		int total = 0;
		int i = 1, min = prices[0];
		while (i < l) {
			if (prices[i] >= prices[i - 1])
				i++;
			else {
				total += prices[i - 1] - min;
				min = prices[i];
				i++;
			}
		}
		total += Math.max(0, prices[l - 1] - min);
		return total;
	}

}